<?php

namespace src\models;

use common\Utils\FormatUtil;
use Yii;
use yii\base\Exception;
use yii\web\NotFoundHttpException;

class Meetings extends \src\baseModels\Meetings
{
    /**
     * @param int $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }


    public function getMethod($isFormat = false)
    {
        return $isFormat ? unserialize($this->method) : $this->method;
    }

    public function getCreatedAt($isFormat = false)
    {
        return FormatUtil::isFormat($isFormat, $this->created_at);
    }

    public function getStartAt($isFormat = false)
    {
        return FormatUtil::isFormat($isFormat, $this->started_at);
    }


    public function getEndAt($isFormat = false)
    {
        return FormatUtil::isFormat($isFormat, $this->end_at);
    }

    /**
     * @return string
     */
    public function getLocationName()
    {
        return $this->location_name;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return \common\db\ActiveQuery
     */
    public static function findByUser()
    {
        return self::find()->where(["user_id" => \Yii::$app->getUser()->getId()]);
    }

    /**
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     * @throws NotFoundHttpException
     */
    public static function findByUserAndId($id)
    {
        $model = self::findByUser()->andWhere(['id' => $id])->one();
        if ($model === null) {
            throw new NotFoundHttpException("会议不存在");
        } else {
            return $model;
        }
    }

    public function clean()
    {
        try {
            Members::clean($this);
            $this->delete();
            return true;
        } catch (Exception $e) {
            return false;
        }
    }


}
